package com.upeoe.shiro.chapter5.service.impl;

import com.upeoe.shiro.chapter5.dao.UserDao;
import com.upeoe.shiro.chapter5.dao.impl.UserDaoImpl;
import com.upeoe.shiro.chapter5.entity.User;
import com.upeoe.shiro.chapter5.service.UserService;
import com.upeoe.shiro.chapter5.utils.PasswordHelper;

import java.util.Set;

/**
 * @author ven_hxz
 * @create 2016-07-05 上午10:04
 */
public class UserServiceImpl implements UserService {

    private UserDao userDao = new UserDaoImpl();
    private PasswordHelper passwordHelper = new PasswordHelper();

    public User createUser(User user) {
        passwordHelper.encryptPassword(user);
        return userDao.createUser(user);
    }

    public void changePassword(Long userId, String newPassword) {
        User user = userDao.findUserById(userId);
        user.setPassword(newPassword);
        passwordHelper.encryptPassword(user);
        userDao.updateUser(user);
    }

    public void addRelationRoles(Long userId, Long... roleIds) {
        userDao.addRelationRoles(userId, roleIds);
    }

    public void removeRelationRoles(Long userId, Long... roleIds) {
        userDao.removeRelationRoles(userId, roleIds);
    }

    public User findByUsername(String username) {
        return userDao.findByUsername(username);
    }

    public Set<String> findRolesByUsername(String username) {
        return userDao.findRoles(username);
    }

    public Set<String> findPermissionsByUsername(String username) {
        return userDao.findPermissions(username);
    }
}
